<?php

/**
 * Yii Controller ProfileController
 *
 * @author nguyenmanhluu
 * @since Aug 1, 2013 - 2:19:31 AM
 */
class ProfileController extends MyController
{

    public function actionIndex()
    {
        $this->checkAccess();
        $model              = Users::model()->findByPk(Yii::app()->user->id);
        $model->nickname    = null;
        $model->description = null;
        $model->setScenario("standard-contact");

        if (isset($_POST['ajax']) && $_POST['ajax'] === 'profile-form')
        {
            echo CActiveForm::validate($model);
            Yii::app()->end();
        }

        if (isset($_POST['Users']))
        {
            /**
             * Upload ảnh mới và xóa ảnh cũ
             */
            $uploadPhoto = $this->_uploadPhoto('user');
            if ($uploadPhoto)
            {
                @unlink($model->photo);
                $model->photo = $uploadPhoto;
            }

            $model->attributes  = $_POST['Users'];
            $birthday           = ($_POST['Users']['birthday'] != '') ? str_replace('/', '-', $_POST['Users']['birthday']) : NULL;
            $model->birthday    = strtotime($birthday);
            $model->nickname    = isset($_POST['Users']['nickname']) ? $_POST['Users']['nickname'] : '';
            $model->description = isset($_POST['Users']['description']) ? $_POST['Users']['description'] : '';
            if ($model->validate() && $model->save())
            {
                Yii::app()->user->setFlash('Success_profile', 'Profile update successfully !');
            }
            else
            {
                Yii::app()->user->setFlash('Error_profile', 'Please fill correct information !');
            }
        }

        $this->render('profile', array('model' => $model));
    }

    public function actionChangePassword()
    {
        $this->checkAccess();

        $model = Users::model()->findByPk(Yii::app()->user->id);
        $model->setScenario('changepassword');
        if (isset($_POST['ajax']) && $_POST['ajax'] === 'changepassword-form')
        {
            echo CActiveForm::validate($model);
            Yii::app()->end();
        }
        if ($model == NULL)
        {
            Yii::app()->user->setFlash('Error_Password', 'Account not found');
        }
        else
        {
            //Mã salt cũ
            if (isset($_POST['Users']))
            {
                $model->attributes = $_POST['Users'];
                if ($model->validate())
                {
                    if ($model->save())
                        Yii::app()->user->setFlash('Success_Password', 'Change Password successfully !');
                }
                else
                {
                    Yii::app()->user->setFlash('Error_Password', 'Please fill correct information');
                }
            }
        }
        $this->render('changepassword', array('model' => $model));
    }

    public function actionChangeEmail()
    {
        $this->checkAccess();

        $model = new Changeemail();
        $admin = Users::model()->findByPk(Yii::app()->user->id);
        $model->setScenario('changeemail');
        if (isset($_POST['ajax']) && $_POST['ajax'] === 'changeemail-form')
        {
            echo CActiveForm::validate($model);
            Yii::app()->end();
        }
        if ($model == NULL)
        {
            Yii::app()->user->setFlash('Error_Password', 'Account not found');
        }
        else
        {
            $salt = $admin->salt;
            if (isset($_POST['Changeemail']))
            {
                $model->attributes = $_POST['Changeemail'];
                $pas_old           = $admin->password;
                $pass              = md5($_POST['Changeemail']['password'] . $salt);
                if ($pass != $pas_old)
                {
                    Yii::app()->user->setFlash('Error_Password', 'The password is not correct');
                }
                else
                {
                    if ($model->validate())
                    {
                        $model->check_id  = md5(date('ymdhi') . crypt(''));
                        $model->email     = $_POST['Changeemail']['newmail'];
                        $model->admin_id  = Yii::app()->user->id;
                        $model->create_at = time();
                        if ($model->save())
                        {
                            $link = Yii::app()->createAbsoluteUrl('user/save') . '?' . 'e=' . $model->email . '&c=' . $model->check_id . '&i=' . $model->admin_id;
                            echo $link;

                            Yii::app()->user->setFlash('Success_ChangeEmail', 'The information has been sent to ' . '<strong>' . $model->email . '</strong>');
                        }
                        else
                        {
                            Yii::app()->user->setFlash('Error_ChangeEmail', 'Please fill correct information');
                            $this->refresh();
                            Yii::app()->end();
                        }
                    }
                    else
                    {
                        Yii::app()->user->setFlash('Error_ChangeEmail', 'Please fill correct information');
                    }
                }
            }
            $this->render('changeemail', array('model' => $model));
        }
    }

    public function actionSave()
    {
        $this->checkAccess();

        $model = Changeemail::model()->find($_GET['i']);
        $admin = Users::model()->findByPk(Yii::app()->user->id);
        if ($admin == NULL)
        {
            Yii::app()->user->setFlash('Error_ChangeEmail', 'Account not found');
        }
        else
        {
            if ($model == Null)
            {
                Yii::app()->user->setFlash('Error_ChangeEmail', 'Link has been activated');
            }
            else
            {
                if ($_GET['c'] == $model->check_id and $_GET['i'] == $model->admin_id)
                {
                    if ($admin->validate())
                    {
                        $admin->email = $model->email;
                        if ($admin->save())
                        {
                            Yii::app()->user->setFlash('Success_ChangeEmail', 'Your Email has been changed' . '<strong>' . $admin->email . '</strong>');
                            if (isset($_GET['i']))
                            {
                                Changeemail::model()->deleteAll(array(
                                    'condition' => 'admin_id =' . '"' . $_GET['i'] . '"',
                                ));
                            }
                        }
                    }
                }
                else
                {
                    Yii::app()->user->setFlash('Error_ChangeEmail', 'Link has been activated Or activation link not correct');
                }
            }
        }
        $this->render('save');
    }

}

/** 
* End file ProfileController
*/
